﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using MySql.Data.MySqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TJ_MainNameSpace
{
    public partial class Form_k45 : Form
    {
        private kongjian45.dianliu3xiang35kv mydianliu;
        private bool loading = false;
        public Form_k45(kongjian45.dianliu3xiang35kv id)
        {
            InitializeComponent();
            this.mydianliu = id;
        }

        //MySqlConnection con = new MySqlConnection(new MySQLConnectionString(TongJi_EMS.Properties.Settings.Default["server"].ToString(), TongJi_EMS.Properties.Settings.Default["database"].ToString(), TongJi_EMS.Properties.Settings.Default["mysqluser"].ToString(), TongJi_EMS.Properties.Settings.Default["mysqlpassword"].ToString(), shujuyuan3.port).AsString);
        MySqlConnection con = new MySqlConnection(TongJi_EMS.Properties.Settings.Default.monitorConnection);

        private void Form1_Load(object sender, EventArgs e)
        {
            loading = true;
            this.textBox1.Text = mydianliu.Name;
            this.textBox2.Text = mydianliu.FaultSignal;
            this.textBox3.Text = mydianliu.Dianliu_ia;
            this.textBox4.Text = mydianliu.Dianliu_ib;
            this.textBox5.Text = mydianliu.Dianliu_ic;
            con.Open();
            //MySqlCommand setchar = new MySqlCommand(@"set names gb2312", con);
            //setchar.ExecuteNonQuery();

            string sql1 = "select distinct db_areaname from dbpoint where project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";

            MySqlDataAdapter oda1 = new MySqlDataAdapter(sql1, con);

            DataSet ds3 = new DataSet();

            oda1.Fill(ds3, "dbpoint");

            this.comboBox1.DataSource = ds3.Tables[0];
            this.comboBox1.DisplayMember = "db_areaname";
            this.comboBox1.ValueMember = "db_areaname";
            con.Close();
            loading = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            //MySqlCommand setchar = new MySqlCommand(@"set names gb2312", con);
            //setchar.ExecuteNonQuery();
            string mysql = "SELECT  ctr_name  FROM  run_ctr where ctr_name='" + mydianliu.Name + "'AND project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";
            //string mysql = "SELECT   COUNT( txt_name='" + mytext.Name + "') FROM  run_temp ";

            MySqlCommand cmd = new MySqlCommand(mysql, con);
            object count = cmd.ExecuteScalar();

            string sql;

            if (count == null)

                sql = "INSERT INTO run_ctr(faultname,ctr_name,ctr_kind,project) VALUES('" + comboBox5.Text + "', '" + textBox1.Text + "' ,'K45','" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "')";
            //sql = "INSERT INTO run_ctr(ctr_name,db_name,ctr_kind) VALUES( '" + textBox1.Text + "' ,'" + kind + "','" + kind + "')";
            else
                sql = "update run_ctr set ctr_name='" + textBox1.Text + "',faultname='" + comboBox5.Text + "' where ctr_name='" + mydianliu.Name + "'AND project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";

            MySqlCommand mInsertCmd = new MySqlCommand(sql, con);
            mInsertCmd.CommandType = CommandType.Text;
            mInsertCmd.ExecuteNonQuery();
            con.Close();
            mydianliu.Name = this.textBox1.Text;
            mydianliu.FaultSignal = this.textBox2.Text;
            mydianliu.Dianliu_ia = this.textBox3.Text;
            mydianliu.Dianliu_ib = this.textBox4.Text;
            mydianliu.Dianliu_ic = this.textBox5.Text;
            this.mydianliu.Refresh();
            this.Close();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loading) return;
            fresh();
        }

        private void fresh()
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            string sql = "select * from dbpoint where project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'AND db_areaname='" + comboBox1.Text + "'";
            MySqlDataAdapter oda = new MySqlDataAdapter(sql, con);

            DataSet ds = new DataSet();
            ds.Clear();
            oda.Fill(ds, "dbpoint");
            this.comboBox2.DataSource = ds.Tables["dbpoint"];
            this.comboBox2.DisplayMember = "db_name";
            this.comboBox3.DataSource = ds.Tables["dbpoint"];
            this.comboBox3.DisplayMember = "db_name";
            this.comboBox4.DataSource = ds.Tables["dbpoint"];
            this.comboBox4.DisplayMember = "db_name";
            this.comboBox5.DataSource = ds.Tables["dbpoint"];
            this.comboBox5.DisplayMember = "db_name";
            con.Close();
        }
    }
}